package com.silence.tool.excel.controller;

import com.silence.tool.common.Result;
import com.silence.tool.excel.domain.input.UnpivotInput;
import com.silence.tool.excel.service.ExcelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author silence
 * @email 792245022@qq.com
 * @create 2023-08-28 16:24
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/excel")
@Tag(name = "Excel 相关接口", description = "Excel 相关接口")
public class ExcelController {

    /**
     * excelService 处理 excel 的业务逻辑
     */
    private final ExcelService excelService;

    /**
     * 行列转换
     *
     * @param input 入参
     * @return 结果
     */
    @PostMapping("/unpivot")
    @Operation(summary = "Excel 反转行列", description = "Excel 反转行列")
    public Result<Void> unpivot(@Validated @RequestBody UnpivotInput input) {
        Pair<Boolean, String> pair = excelService.unpivot(input);
        return Result.<Void>builder()
                .code(pair.getLeft() ? 200 : 500)
                .msg(pair.getRight())
                .build();
    }

}
